<?php
class TicketModel extends Model {
	public $list;
    public function _list($view,$join = '', $field,$map,$rename = 'r',$pagesize=20) {
		//计算总页数
		$count = $this->alias($rename)->join($join)->where($map)->count();
		import('ORG.Util.Page');
		//实例化分页类
		$p = new Page($count, $pagesize);
		$list = $this->alias($rename)->join($join)->where($map)->Field($field)->limit($p->firstRow . ',' . $p->listRows)->select();
		$pages = $p->show();
		$_GET['list']=$list;
		$view->assign('list',$list);
		$view->assign('pages',$pages);
    }
	public function filter($view){
        //筛选开始时间与结束时间
        if(trim($_REQUEST['starttime'])!=''){
            $map['r.time']=array('GT',strtotime($_REQUEST['starttime']));
        }
        if(trim($_REQUEST['endtime'])!=''){
            $map['r.time']=array('LT',strtotime($_REQUEST['endtime']));
        }
        if(""==trim($_REQUEST['starttime'])&&""==trim($_REQUEST['endtime'])){
            $map['r.time']=array('GT',strtotime("-1 month"));
        }
        if(""!=trim($_REQUEST['starttime'])&&""!=trim($_REQUEST['endtime'])){
            $map['r.time']=array('between',array(strtotime($_REQUEST['starttime']),strtotime($_REQUEST['endtime'])));
        }
        //筛选某个职员 或一整个部门
        if(""!=trim($_REQUEST['member_id'])){
            $map['r.member_id']=$_REQUEST['member_id'];    
        }else{
            if(""!=trim($_REQUEST['groupid'])){
                $map['g2.id']=$_REQUEST['groupid'];
            }else{					
                $map['g2.pid']=0;
            }
        }
        $map['r.site_id']=__SITEID__;
		
		$join=['left join __MEMBER__ m on r.member_id=m.id','left join __MEMBER__ m2 on r.giver_id=m2.id','left join __MEMBER__ m3 on r.rewarder_id=m3.id','left join __MEMBER_GROUP__ g on g.id=m.group_id','left join __MEMBER_GROUP__ g2 on g2.id=g.pid'];
		$field='r.id,r.number,r.remark,r.time,m.name,m2.name as givername,m3.name as rewardername,g2.title as group_title,g2.id as group_id,r.rewarder_id';
		
		$view->assign('totalnum',$this->sumall($join,$map));
		$this->_list($view,$join,$field,$map);
	}
	public function sumall($join,$map,$field='r.number'){
		return $this->alias('r')->join($join)->where($map)->sum($field);	
	}
	
	
	
	//分配奖票
	public function sendticket($member_id, $tomember_id, $num, $option = array()){
		
		$member = M('Member') -> where(array('id' => $member_id, 'site_id' => __SITEID__)) -> find();
		if($member['tiketnum'] < 0){
			$this->error = '您没有权限分配奖票 ！';
			return false;
		}
		
		
		//查看这个月发了多少奖票了
		//这个月的时间区间
		$start = time();
		$start=strtotime(date('Y')."-".date('m')."-1");
        $map['time'] = array('between',array($start,$end));
		$map['rewarder_id'] = $member['id'];
		$map['giver_id'] = $member['id'];
		$count = (int)M('Ticket') -> where($map) -> sum('number');
		
		
	
		//dump($count);
		
		
		//设置了数量说明限数量，
		if($member['tiketnum'] > 0 && $member['tiketnum'] >= $count){
			$this->error = '您当前月份分配的奖票已经超出上限了 ！';
			return false;
		}
		//查找记录
		$tmap['mission_id'] = isset($option['mission_id']) ? $option['mission_id'] : 0;
		$tmap['info_id'] = isset($option['info_id']) ? $option['info_id'] : 0;
		$tmap['giver_id'] = $member['id'];
		$tmap['rewarder_id'] = $member['id'];
		$tdb = M('Ticket');
		//dump($num);
		$ticket = $tdb -> where($tmap) -> find();
		//dump($num);
		if($num == 0){
			//删除记录
			if($ticket){
				$tdb -> where($tmap) -> delete();
			}
//			dump($num);exit;
			return false;
		}
		//状态正常，发放奖票
		
		
		
		
		
		
		
		
		if(!$ticket){
			
			$data['time'] = time();
			$data['member_id'] = $tomember_id;
			$data['number'] = $num;
			$data['info_id'] = isset($option['info_id']) ? $option['info_id'] : 0;
			$data['mission_id'] = isset($option['mission_id']) ? $option['mission_id'] : 0;
			$data['site_id'] = __SITEID__;
			if($data['mission_id'] > 0){
				$data['remark'] = '发布动态奖票#'.$data['info_id'].'#';
			}
			if($data['mission_id'] > 0){
				//找到这个审批
				$data['remark'] = '完成任务#'.$data['mission_id'].'#';
			}
			
			$data['giver_id'] = $member['id'];
			$data['rewarder_id'] = $member['id'];
			$ret = $tdb -> add($data);
		}else{
			$data['number'] = $num;
			$ret = $tdb -> where($tmap) -> save($data);
		}
		
		
		if($ret){
			return true;
		}else{
			$this->error = $tdb -> getError();
			return false;
		}
	}
	
}